package 获取锁对象;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket implements Runnable {
	int ticket = 100;
	// 获取锁对象
	Lock lo = new ReentrantLock();// 重新入锁

	@Override
	public void run() {
		while(true) {
		 if(ticket==0) {
			 System.out.println("票卖完了");
			break;
		 }lo.lock();//获取锁
		 if(ticket>0) {
			 try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }System.out.println(Thread.currentThread().getName()+"正在卖票"+ticket--);
	} lo.unlock();//释放锁
	}

}
